package com.qf.controller;

import com.qf.entity.Address;
import com.qf.entity.GoodCar;
import com.qf.entity.User;
import com.qf.service.AddressService;
import com.qf.service.GoodCarService;
import com.qf.service.GoodService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.GoodCarServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author AzureSky_X
 * @Date 2020/11/18 17:41
 * @Version 1.0
 */
@WebServlet("/car")
public class GoodCarController extends BaseController {
    private GoodCarService service=new GoodCarServiceImpl();
    private AddressService addressService=new AddressServiceImpl();
    public String addCart(HttpServletRequest request, HttpServletResponse response){
        String strGoodId = request.getParameter("goodId");
        String strPrice = request.getParameter("price");
        GoodCar car=new GoodCar();
        car.setGoodId(Integer.parseInt(strGoodId));
        //默认添加一个商品
        car.setGoodNum(1);
        //省略写法，价格X数量，但是数量默认为1
        car.setGoodSum(new BigDecimal(strPrice));
        User user = (User) request.getSession().getAttribute("loginUser");
        car.setUid(user.getId());
        int result=service.addCart(car);
        if(result==1){
            System.out.println("添加购物车成功");
            return "redirect:cartSuccess.jsp";
        }else{
            request.setAttribute("msg","添加购物车失败");
            return "forward:error/error.jsp";
        }

    }

    /**
     * 查询该用户所有购物车信息的方法
     * @param request
     * @param response
     * @return
     */
    public String getCarts(HttpServletRequest request, HttpServletResponse response){

        User user = (User) request.getSession().getAttribute("loginUser");
        if(user==null){
            return "redirect:login.jsp";
        }
        List<GoodCar> list=service.getCarts(user.getId());
        request.setAttribute("carts",list);
        return "forward:cart.jsp";
    }

    public String updateCartNum(HttpServletRequest request, HttpServletResponse response){
        //拿参数
        String strGoodId = request.getParameter("pid");
        String strPrice = request.getParameter("price");
        String strNum=request.getParameter("num");//表示的是执行的动作
        User user = (User) request.getSession().getAttribute("loginUser");
        int result=service.updateCartNum(Integer.parseInt(strGoodId),new BigDecimal(strPrice),Integer.parseInt(strNum),user.getId());

        return String.valueOf(result);
    }


    public String clearCart(HttpServletRequest request, HttpServletResponse response){
        String strId = request.getParameter("id");
        int result=service.clearCart(Integer.parseInt(strId));
        if(result==0){
            request.setAttribute("msg","删除失败");
            return "forward:error/error.jsp";
        }else{
            return "redirect:car?action=getCarts";
        }

    }


    /**
     * 清空购物车
     * @param request
     * @param response
     * @return
     */
    public String clearAllCar(HttpServletRequest request,HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("loginUser");
        int result=service.clearAllCar(user.getId());
        if(result==0){
            request.setAttribute("msg","清空失败");
            return "forward:error/error.jsp";
        }else{
            return "redirect:good?action=getGoodsListByTypeId";
        }
    }

    /**
     * 生成数据去订单预览界面
     * @param request
     * @param response
     * @return
     */
    public String getOrderView(HttpServletRequest request,HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("loginUser");
        //根据用户id拿到该人员下面所以的购物车信息
        List<GoodCar> carts = service.getCarts(user.getId());
        request.setAttribute("cartList",carts);
        //还需要查询出该人员他的地址信息列表
        List<Address> list=addressService.getAddressList(user.getId());
        request.setAttribute("addList",list);
        return "forward:order.jsp";
    }
}
